package com.shanshi.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.shanshi.activity.DelteListViewItme.DelButtonClickListener;
import com.shanshi.activity.adapter.MyCollectAdapter;
import com.shanshi.activity.response.MyCollectInfo;
import com.shanshi.activity.tool.OkHttpTool;

import java.util.ArrayList;
import java.util.List;


public class Mycollect extends MainActivity implements OnItemClickListener,OnClickListener{

	private SharedPreferences sharedPreferences;
	private DelteListViewItme myCollectlv;
	private TextView tv;
	private MyCollectAdapter myCollectAdapter;
	private List<MyCollectInfo.Data> list=new ArrayList<>();
	private List<MyCollectInfo.Data> listdata=new ArrayList<>();
	private ImageView Iv;
	private ArrayAdapter<String> mAdapter;
	private int pageIndex=0;
	private int userId;
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.mycollects);

		sharedPreferences = Mycollect.this.getSharedPreferences("share", MODE_PRIVATE);
		userId = sharedPreferences.getInt("userId",4);

		myCollectlv =(DelteListViewItme)findViewById(R.id.myCollectViews);
		
		tv = (TextView) findViewById(R.id.title);
		tv.setText("我的收藏");
		Iv = (ImageView) findViewById(R.id.fanhui);
		Iv.setOnClickListener(this);

		myCollectAdapter=new MyCollectAdapter(this, listdata);
		
		myCollectlv.setAdapter(myCollectAdapter);

		//获得初始化数据
		getdatas();
		
		myCollectlv.setDelButtonClickListener(new DelButtonClickListener(){

			@Override
			public void clickHappend(int position) {
				
				//执行删除操作
				int articleId = list.get(position-1).getArticleId();
				list.remove(myCollectAdapter.getItem(position-1));
				handler.sendEmptyMessage(1);
				cancelCollect(articleId);
			}
			
		});
		myCollectlv.setOnRefreshListener(new DelteListViewItme.OnRefreshListener() {
			@Override
			public void onPullRefresh() {
						listdata.clear();
						pageIndex = 0;
						getdatas();

			}

			@Override
			public void onLoadingMore() {
						pageIndex++;
						getdatas();
			}
		});
	
	}

	//获取数据
	public void getdatas(){
		OkHttpTool.doGet(OkHttpTool.BASE_URL+"users/"+userId+"/collectionArticles?pageIndex="+pageIndex+"&pageSize=4"
				, new OkHttpTool.setOkhttpListener() {
					@Override
					public void onSuccess(String s) {
						// TODO Auto-generated method stub
						list.clear();
						Gson gson=new Gson();
						MyCollectInfo content=gson.fromJson(s, MyCollectInfo.class);
						Log.e("=====result",content.toString());
						list.addAll(content.getData());
						handler.sendEmptyMessage(1);
					}

					@Override
					public void onFail() {
						// TODO Auto-generated method stub
						Log.e("=========我的收藏请求失败","sadfa");
					}
				});

	}


	//取消收藏发送请求
	public void cancelCollect(int articleId){
		OkHttpTool.doDelete(OkHttpTool.BASE_URL+"users/"+userId+"/articles/"+articleId+"/collection"
				, "",new OkHttpTool.setOkhttpListener() {
					@Override
					public void onSuccess(String s) {

					}

					@Override
					public void onFail() {
						// TODO Auto-generated method stub
						Log.e("=========sa","error");
					}
				});
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		
	}

	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		
		case R.id.fanhui:
			
			finish();
			
			break;

		}
		
	}

	Handler handler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
				case 1:
					if (list.size()!=0){
					listdata.addAll(list);
					myCollectAdapter.notifyDataSetChanged();
					myCollectlv.completeRefresh();
				}else {
						Toast.makeText(Mycollect.this,"已经是最后一页",Toast.LENGTH_SHORT).show();
						myCollectlv.completeRefresh();
					}
					break;
			}
		}
	};

}
